<?php

namespace app\api\controller\v1\plan;

use app\model\course\course\Course;
use app\model\course\topic\Topic;
use app\model\course\ware\Courseware;
use app\model\plan\course\StudyCourse;
use app\model\plan\Plan;
use app\model\plan\PlanCourse;
use app\model\plan\PlanStaff;
use app\model\plan\snap\Snap;
use app\model\task\TaskCert;
use app\Request;
use app\services\face\CertService;
use app\services\plan\PlanService;
use think\facade\Db;

class PlanController
{
    public function aa()
    {
        $service = new CertService() ;
        $ret = $service->jieye(27 ,63 , 0) ;
        if($ret == true){
            return $service->getImageUrl() ;
        }
        return $service->getError() ;
    }

    public function course(Request $request)
    {
        list($planId) = $request->getMore([
            ['plan_id', 0] ,
        ], true);

        $where[] = ['id' ,'=', $planId ] ;
        $where[] = ['staff_id' ,'=', $request->staffId() ] ;
        $planStaff = PlanStaff::where($where)->find() ;
        if($planStaff == []){
            return app('json')->fail(['课程不存在']) ;
        }
        $plan = Plan::where('id' , $planStaff['plan_id'])->find() ;
        $list = [] ;

        $planService = new PlanService() ;
        $courseList = $planService->courseList($plan['id'] , $request->staffId()) ;
        $stat = $planService->stat($plan['id'] , $request->staffId()) ;

        return app('json')->success([
            'list' => $courseList ,
            'stat' => $stat
        ]);

    }
    public function courseware(Request $request){
        list($wareId  , $planStaffId) = $request->getMore([
            ['ware_id', 0] ,
            ['staff_plan_id', 0] ,
        ], true);

        $planStaff = PlanStaff::where('id' , $planStaffId)->find() ;

        $info = Courseware::getOne($wareId) ;
        if($info == null){
            return app('json')->fail(['课程不存在']) ;
        }

        if($planStaff['study_start_time'] == 0 || $planStaff['study_start_time'] == ''){
            $planStaff->study_start_time = time() ;
            $planStaff->save() ;
        }

        $where[] = ['courseware_id' , '=' , $wareId] ;
        $where[] = ['plan_id' , '=' , $planStaff['plan_id']] ;
        $where[] = ['plan_staff_id' , '=' , $planStaffId] ;
        $where[] = ['staff_id' , '=' , $request->staffId()] ;
        $info2 = StudyCourse::where($where)->find() ;
        $plan = Plan::getOne($planStaff['plan_id']) ;
        if($info2 == null){
            $info2 = StudyCourse::create([
                'courseware_id' => $wareId ,
                'plan_id' => $planStaff['plan_id'],
                'plan_staff_id' => $planStaffId ,
                'staff_id' => $request->staffId() ,
                'study_status' => 1 ,
                'courseware_hour' => $info['hour'] ,
            ]) ;
        }
        $snapStatus = $request->staffInfo()->snap_status ;



        $surSnap = $plan['courseware_snap_count']  ;
        if($snapStatus == 1){
            $list = Snap::snapImage($planStaff['plan_id'] , $request->staffId() , Snap::TYPE_POST) ;
            $sur = $plan['snap_count'] - count($list)  ;

            //总次数 - 总抓拍 ；
            //剩余抓拍次数
            $sur = $sur > 0 ? $sur : 0 ;
            if($sur == 0){
                $surSnap = 0 ;
            }
            else{
                if($sur <= $plan['courseware_snap_count'] ){
                    $surSnap = $sur ;
                    $surSnap = $surSnap ? $surSnap : 0 ;
                }
                else{
                    $surSnap = $plan['courseware_snap_count']  ;
                }
            }

            if($surSnap <= 0){
                $snapStatus = 0 ;
            }
        }

        return app('json')->success([
            'info' => [
                'video_url' => $info['video_url'] ,
                'title' => $info['title'] ,
                'hour' => $info['hour'] ,
                'duration' => $info['duration'] ,
                'topic_open' => $info['topic_open'] ,
                'video_progress_time' => $info2['video_progress_time'] ,
            ] ,
            'study_id' =>$info2['id'] ,
            'snap_total' => $surSnap ,
            'scope_time' => $plan['interval_time'] * 60 ,
            'user' => [
                'speed_status' => $request->staffInfo()->speed_status ,
                'snap_status' => $snapStatus ,
            ] ,
        ]);
    }
    public function test_topic(Request $request)
    {
        list($wareId , $planId) = $request->getMore([
            ['courseware_id', 0] ,
            ['plan_id', 0] ,
        ], true);

        $info = Courseware::getOne($wareId) ;
        $list = Topic::examTopic($info['topic_ids']);

        return app('json')->success([
            'list' => $list
        ]);
    }
    public function test_topic_inc(Request $request)
    {
        list($wareId , $topicId , $planId) = $request->getMore([
            ['courseware_id', 0] ,
            ['topicId', 0] ,
        ], true);
        return app('json')->success();
    }
    public function sign( Request $request )
    {
        list($planId , $imgUrl) = $request->getMore([
            ['plan_id', 0] ,
            ['img_url', 0] ,
        ], true);

        $where[] = ['id' ,'=', $planId ] ;
        $where[] = ['staff_id' ,'=', $request->staffId() ] ;
        $planStaff = PlanStaff::where($where)->find() ;
        if($planStaff == []){
            return app('json')->fail('课程不存在') ;
        }
        if($planStaff['complete_status'] == 10){
            return app('json')->fail('您已签字，不可重复签字') ;
        }
        if($planStaff['complete_status'] != 9){
            return app('json')->fail('当前状态不可签字') ;
        }


        $list = Snap::snapImage($planStaff['plan_id'] , $request->staffId() , Snap::TYPE_POST) ;
        if(count($list) < 3){
            return app('json')->fail('抓拍不够3张，不可签字') ;
        }


        try {
            Db::startTrans() ;
            $planStaff->complete_status = 10 ;
            $planStaff->task_status = 1 ;
            $planStaff->sign_time = time() ;
            $planStaff->sign_img_url = $imgUrl ;
            $planStaff->save() ;

            TaskCert::create([
                'plan_staff_id' => $planStaff['id'] ,
                'staff_id' => $planStaff['staff_id'] ,
                'plan_id' => $planStaff['plan_id'] ,
                'task_status' => 1,
            ]) ;
            Db::commit() ;
            return app('json')->success('签字成功') ;

        }
        catch (\Exception $e){
            return app('json')->success($e->getMessage()) ;
        }
    }
    public function staff(Request $request)
    {
        list($planId) = $request->getMore([
            ['plan_id', 0] ,
        ], true);

        $where[] = ['id' ,'=', $planId ] ;
        $where[] = ['staff_id' ,'=', $request->staffId() ] ;
        $planStaff = PlanStaff::where($where)->find() ;
        if($planStaff == []){
            return app('json')->fail('课程不存在') ;
        }
        $plan = Plan::getOne($planStaff['plan_id']) ;

        return app('json')->success([
            'info' => [
                'plan_name' => $planStaff->plan->plan_name ,
                'time' => date('Y-m-d' , $planStaff->plan->start_time ) . ' 至 ' . date('Y-m-d' , $planStaff->plan->end_time ) ,
                'study_end_time' => $planStaff->study_end_time ,
                'exam_point' => $planStaff->exam_point ,
                'exam_status' => $plan->exam_id > 0  ? 1 : 2 ,
            ]
        ]) ;
    }
}
